/**
 * localStorage 数据存储
 * alon - 2021-02-22
 */
export const constants = {
  USER_LOGIN_INFO: "USER_LOGIN_INFO", //登录信息
  ADMIN_TOKEN: "ADMIN_TOKEN", //token
  WX_OPENID: "WX_OPENID", //活动详情信息
  appId: "appId", //活动详情信息
  siteName: "siteName", //网站名称
  webapp_info: "webapp_info", //网站信息
  oauth: "oauth",
  permissions: "permissions",
  clientId: "clientId",
  scope: "scope",
  statisticsStr: "statisticsStr",
  tempCameristInfo: "tempCameristInfo",
  theme: "theme", //主题
  loginState: "loginState", //登录状态，含登录时间与登录失败次数
  localStoragePageSize: "localStoragePageSize", //分页条数
  datasTime: "datasTime", //订单默认时间
  webappLeft: "webappLeft", //左边导航栏收缩
  globalPageSize: "globalPageSize", //全局颁布数量
  //新
  areaList: 'areaList',//区域位置
  nationList: 'nationList',//民族
  school_type: 'school_type',//获取学校类别
  school_class: 'school_class',//获取教育类型
  schoolList: 'schoolList',//学校列表信息
};

const SESSION_KEY = "gzyl_scias_local_";
/**
 * sessionStorage
 */
export const Session: any = {
  get: (keys: string) => {
    let se_key = SESSION_KEY + constants.USER_LOGIN_INFO;
    if (keys) {
      if (keys == "appId" || keys == "clientId") {
        se_key = SESSION_KEY + keys;
      } else {
        se_key = SESSION_KEY + getAppId() + "_" + keys;
      }
    }
    let arr = localStorage.getItem(se_key);
    return arr ?? null;
  },
  set: (keys: string, session: string) => {
    let se_key = SESSION_KEY + constants.USER_LOGIN_INFO;
    if (keys) {
      if (keys == "appId" || keys == "clientId") {
        se_key = SESSION_KEY + keys;
      } else {
        se_key = SESSION_KEY + getAppId() + "_" + keys;
      }
    }
    return localStorage.setItem(se_key, session);
  },
  clear: (keys: string) => {
    let se_key = SESSION_KEY + constants.USER_LOGIN_INFO;
    if (keys) {
      if (keys == "appId" || keys == "clientId") {
        se_key = SESSION_KEY + keys;
      } else {
        se_key = SESSION_KEY + getAppId() + "_" + keys;
      }
    }
    return localStorage.removeItem(se_key);
  },
  clearAll: () => {
    return localStorage.clear();
  },
};
/**
 * 获取APPID或者clientId
 * @returns
 */
const getAppId = () => {
  let appId: any = localStorage.getItem(SESSION_KEY + constants.appId) || "";
  let clientId: any = localStorage.getItem(SESSION_KEY + constants.clientId) || "";
  if (appId) {
    return appId;
  } else if (clientId) {
    return clientId;
  } else {
    return "";
  }
};
